#!/bin/bash
set -x

BASE_DIR=/ci/env-deploy
CODE_DIR=/ci/deploy/code
TMP_DIR=/ci/env-deploy/tmp
REMOTE_DIR=/opt/dd/

# recive params
# env project branch
if [[ $# -ne 3 ]];then
  echo "deploy.sh env project repo branch"
  exit 1
fi

env=${1}
app=${2}
repo=${3%/*}
branch=${3##*/}

# from mysql get deploy parameters
python3 ${BASE_DIR}/utils/get_deploy_parameters.py ${env} ${app}
[ $? == 0 ] || exit 1

# source parameters to current session
if [[ -f ${BASE_DIR}/tmp/${env}-${app}.sh ]];then
  source ${BASE_DIR}/tmp/${env}-${app}.sh
  rm -f ${BASE_DIR}/tmp/${env}-${app}.sh
else
  echo "not get ${env} ${app} parameters, please check env database"
  exit 1
fi

## set nginx
#source ${BASE_DIR}/utils/set_nginx.sh

# set git(进入到project目录)
source ${BASE_DIR}/utils/set_git.sh

# replace parameters

# compile
case ${deploy_type} in
  "tomcat")
    [ -z ${sub_modle} ] || cd ${sub_modle}
    mvn clean source:war package -Dmaven.test.skip=true
    # check package format and make sure it exists

    ;;
  "spring")
    [ -z ${sub_modle} ] || cd ${sub_modle}
    mvn clean source:jar package -Dmaven.test.skip=true
    # check package format and make sure it exists

    ;;
  "static")
    if [[ -f ${BASE_DIR}/scripts/fe/${app}.sh ]];then
      source ${BASE_DIR}/scripts/fe/${app}.sh || exit 1
      exit 0
    fi
    if [[ -f ${CODE_DIR}/fe/${env}/${app}/build.sh ]];then
      source ${CODE_DIR}/fe/${env}/${app}/build.sh || exit 1
      exit 0
    fi
    npm install --registry=http://registry.npm.taobao.org || exit 1
    TEST_ENV=${env} npm run release || exit 1
    rsync -acpz --rsh=\'ssh -o StrictHostKeyChecking=no\' --delete ${CODE_DIR}/fe/${env}/${app}/* root@${host}:${REMOTE_DIR}/fe/${app} --exclude=.git > /dev/null || exit 1
    exit 0
    ;;
  "*")
    echo "not found this deploy type, maybe not support"
    exit 1
esac

# ssh kill service
ssh -o StrictHostKeyChecking=no root@${host} "
if [[ -d ${REMOTE_DIR} ]];then
    pidfile=${REMOTE_DIR}/${app}/run.pid
    if [[ -s \${pidfile} ]] && [[ \`cat \${pidfile} | xargs ps | sed -n '2P' | wc -l\` -gt 0 ]];then
      kill -9 \`cat \${pidfile}\`
    else
      ps -ef| grep \"\${app}\" | grep -v 'grep' | awk '{print \$2}' | xargs kill -9
    fi
    sleep 3
    [[ -d ${REMOTE_DIR}/${app} ]] && rm -rf ${REMOTE_DIR}/${app}
else
    mkdir -p ${REMOTE_DIR}
fi
"

# scp package to target host
if [[ ! -z ${app_pkg} -a -f ${app_pkg} ]];then
  # tomcat 需要提前放置模板
  if [[ ${deploy_type} == "tomcat" ]];then
    [ -d ${BASE_DIR}/tmp/${env}-${app} ] || mkdir -p ${BASE_DIR}/tmp/${env}-${app}
    \cp -rf ${BASE_DIR}/model/tomcat/* ${BASE_DIR}/tmp/${env}-${app}
    rm -f ${BASE_DIR}/tmp/${env}-${app}/conf/server.xml
    mv ${BASE_DIR}/tmp/${env}-${app}/conf/server.online.xml ${BASE_DIR}/tmp/${env}-${app}/conf/server.xml
    [ -z ${shutdownport} ] && echo "not found tomcat shutdown port" && exit 1
    sed -i "s/shutdownport/${shutdownport}/g" ${BASE_DIR}/tmp/${env}-${app}/conf/server.xml
    sed -i "s/listenport/${listenport}/g" ${BASE_DIR}/tmp/${env}-${app}/conf/server.xml
    scp -o StrictHostKeyChecking=no -r ${BASE_DIR}/tmp/${env}-${app} root@${host}:${REMOTE_DIR}/${app} || (rm -rf ${BASE_DIR}/tmp/${env}-${app} && exit 1)
    rm -rf ${BASE_DIR}/tmp/${env}-${app}
    scp -o StrictHostKeyChecking=no ${app_pkg} root@${host}:${REMOTE_DIR}/${app}/${app}.war || exit 1
  else
    scp -o StrictHostKeyChecking=no ${app_pkg} root@${host}:${REMOTE_DIR}/${app}/${app}.jar || exit 1
  fi
  [[ ! -z ${app_source_pkg} -a -f ${app_pkg} ]] && scp -o StrictHostKeyChecking=no ${app_source_pkg} root@${host}:${REMOTE_DIR}/${app}/${app}_source.jar
else
  echo "can not find jar package, please check it !"
  exit 1
fi

# startup
case ${deploy_type} in
  "tomcat")
    source ${BASE_DIR}/utils/start_tomcat.sh
    ;;
  "spring")
    source  ${BASE_DIR}/utils/start_spring_boot.sh
    ;;
  "*")
    echo "not support this deploy type .."
    exit 1
    ;;
esac
# monitor port
set +x
for ((i=0;i<30;i++))
do
  sleep 10
  nc -v -w 1 ${host} ${listenport}
  [ $? -eq 0 ] && exit 0
done
set -x
echo "can not connect to ${host}:${listenport}, please check startup log file!"
echo "print startup log file last 500 lines..."
ssh root@${host} "tail -500 ${REMOTE_DIR}/${app}/startup.log"
exit 1
